import numpy as np
import partie1_methodes as meth
import matplotlib.pyplot as mp

def Malthus():
    
    nmax = 200
    x = np.arange(0.,10.+10./nmax,10./nmax)

    mp.clf()
    birth = 3.
    death = 2.
    eq = [(0., [7e9]), lambda X,t:np.array([(birth-death)*X[0]])]
    res = meth.meth_n_step(eq, nmax, 10./nmax, meth.step_RK4)
    g1, = mp.plot(x,res,linewidth=1.0)
    mp.title("Malthus avec birth-death > 0")
    mp.savefig("../img/Malthus_birth_death_pos")
    mp.show()
    
    birth2 = 2.
    death2 = 3.
    eq2 = [(0., [7e9]), lambda X,t:np.array([(birth2-death2)*X[0]])]
    res2 = meth.meth_n_step(eq2, nmax, 10./nmax, meth.step_RK4)
    g2, = mp.plot(x,res2,linewidth=1.0)
    mp.title("Malthus avec birth-death < 0")
    mp.savefig("../img/Malthus_birth_death_neg")
    mp.show()

def Verhulst():

    nmax = 200
    x = np.arange(0.,10.+10./nmax,10./nmax)
    
    mp.clf()
    gamma = 1. 
    k = 20e9
    eq = [(0., [7e9]), lambda X,t:np.array([gamma*X[0]*(1 - X[0]/k)])]
    res = meth.meth_n_step(eq, nmax, 10./nmax, meth.step_RK4)
    g1, = mp.plot(x,res,linewidth=1.0)

    gamma = 1.
    k = 10e9
    eq2 = [(0., [7e9]), lambda X,t:np.array([gamma*X[0]*(1 - X[0]/k)])]
    res2 = meth.meth_n_step(eq2, nmax, 10./nmax, meth.step_RK4)
    g2, = mp.plot(x,res2,linewidth=1.0)

    mp.title("Methode Verhulst")
    mp.legend((g1,g2),("Verhulst gamma>0 / k = 20e9","Verhulst gamma > 0 / k =10e9"))
    mp.savefig("../img/Verhulst")
    mp.show()

def LotkaVolterra():

    nmax = 200
    x = np.arange(0.,10.+10./nmax,10./nmax)

    mp.clf()
    a = 1.
    b = 0.2
    c = 0.04
    d = 0.5

    eq = [(0.,[0.5,1.]), lambda X,t:np.array([X[0]*(a-b*X[1]),X[1]*(c*X[0]-d)])]
    res = meth.meth_n_step(eq, nmax, 50./nmax, meth.step_RK4)
    tabres0 = map(lambda t: t[0], res)
    tabres1 = map(lambda t: t[1], res)

    g1, = mp.plot(x,tabres0,linewidth=1.0)
    g2, = mp.plot(x,tabres1,linewidth=1.0)

    mp.legend((g1,g2),("Proies","Predateurs"))
    mp.title("Proies > Predateurs")
    mp.savefig("../img/ProiesSupPred")
    mp.show()

    mp.clf()
    a = 0.2
    b = 0.4
    c = 0.7
    d = 0.5
    nmax = 100
    
    for i in range(20):
            eq = [(0.,[0.+0.1*i,0.+0.1*i]), lambda X,t:np.array([X[0]*(a-b*X[1]),X[1]*(c*X[0]-d)])]
            res = meth.meth_n_step(eq, nmax, 40./nmax, meth.step_RK4)
            tabres0 = map(lambda t: t[0], res)
            tabres1 = map(lambda t: t[1], res)
            g1, = mp.plot(tabres0,tabres1,linewidth=1.0)
    mp.axis([0.,3.,0.,3.5])
    mp.title("Predateurs en fonction de proies")
    mp.savefig("../img/PredFoncProies")
    mp.show()
    
    mp.clf()
    a = 1.
    b = 1.
    c = 15. #tu peux essayer ici avec 20
    d = 1.
    nmax = 200
    
    eq = [(0.,[1.,1.]), lambda X,t:np.array([X[0]*(a-b*X[1]),X[1]*(c*X[0]-d)])]
    res = meth.meth_n_step(eq, nmax, 50./nmax, meth.step_RK4)
    tabres0 = map(lambda t: t[0], res)
    tabres1 = map(lambda t: t[1], res)

    g1, = mp.plot(x,tabres0,linewidth=1.0)
    g2, = mp.plot(x,tabres1,linewidth=1.0)
    
    mp.legend((g1,g2),("Proies","Predateurs"))
    mp.title("Proies < Predateurs")
    mp.savefig("../img/ProiesInfPred")
    mp.show()

    mp.clf()
    a = 0.2
    b = 0.4
    c = 0.7
    d = 0.5

    eq = [(0.,[d/c,a/b]), lambda X,t:np.array([X[0]*(a-b*X[1]),X[1]*(c*X[0]-d)])]
    res = meth.meth_n_step(eq, nmax, 50./nmax, meth.step_RK4)
    tabres0 = map(lambda t: t[0], res)
    tabres1 = map(lambda t: t[1], res)

    g1, = mp.plot(x,tabres0,linewidth=1.0)
    g2, = mp.plot(x,tabres1,linewidth=1.0)
    mp.axis([0.,1.,0.,1.])
    mp.legend((g1,g2),("Proies","Predateurs"))
    mp.title("Solutions constantes")
    mp.savefig("../img/ConstSolLotka")
    mp.show()
